﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using UboxUI;
using User.Properties;

namespace User
{
    public partial class FrmMessageControl : FrmFather
    {
        public FrmMessageControl()
        {
            InitializeComponent();
            this.LblTitle.Text = this.Text;
            this.LblTitle2.Text = LblTitle.Text;
            this.BtnClose.Left = this.Width - this.BtnClose.Width - 10;
            this.BtnNarrow.Left = this.BtnClose.Left - this.BtnNarrow.Width - 10;
            this.panel1.Top = 0;
            this.panel2.Top = this.panel1.Height;
        }

        #region 初始宣告
        string conn = Settings.Default.strConn;
        bool btnRptClick = false; // 辨認用字別
        #endregion


        private void FrmMessageControl_Load(object sender, EventArgs e)
        {
            DataGridViewFill();
        }


        private void Range(DataGridView dataGV)
        {
            dataGV.Columns["編號"].Width = 70;
            dataGV.Columns["編號"].Name = "編號";
            dataGV.Columns["發表日期"].Width = 250;
            dataGV.Columns["內容"].Width = 250;
            dataGV.Columns["用戶帳號"].Width = 160;
            dataGV.Columns["發表於開箱文"].Width = 160;
            this.DataGridView.Columns["發表日期"].DefaultCellStyle.Format = "yyy/MM/dd  tt hh:mm:ss";
        }

        private void DataGridViewFill()
        {
            using (UnboxingLinqtoSqlDataContext db = new UnboxingLinqtoSqlDataContext(conn))
            {
              
                int unboxingID = 0;
                int.TryParse(this.txtUnboxingID.Text, out unboxingID);
                var q = from m in db.message_board
                        join r in db.user_report on m.message_id equals r.report_message_id into temp
                        from m1 in temp.DefaultIfEmpty()
                        where
                        (btnRptClick ? m.message_id == m1.report_message_id : true) &&
                        (chkUnboxingID.Checked ? unboxingID == m.unboxing_article_id : true) &&
                        (chkUser.Checked ? this.txtUser.Text == m.user.user_username : true) &&
                        (chkDate.Checked ? m.message_date >= this.dtpPulishDate1.Value && m.message_date <= this.dtpPulishDate2.Value : true)
                        select new
                        {
                            可見度 = m.message_visible.ToString(),
                            編號 = m.message_id,
                            發表日期 = m.message_date,
                            內容 = m.message_content,
                            用戶帳號 = m.user.user_username,
                            發表於開箱文 = m.unboxing_article_id,
                            
                        };



                this.DataGridView.DataSource = q.ToList();



                Range(this.DataGridView);

            }


        }


        private void btnReport_Click(object sender, EventArgs e)
        {
            btnRptClick = true;
            DataGridViewFill();
            btnRptClick = false;
        }

        private void btnSelect_Click(object sender, EventArgs e)
        {
            DataGridViewFill();
        }

        private void btnhide_Click(object sender, EventArgs e)
        {
            DialogResult result = MessageBox.Show("改變留言顯示狀態", "系統提醒", MessageBoxButtons.YesNo, MessageBoxIcon.Warning);
            if (result == DialogResult.Yes)
            {
                int rowCount = this.DataGridView.SelectedRows.Count;
                using (UnboxingLinqtoSqlDataContext db = new UnboxingLinqtoSqlDataContext(conn))
                {
                    for (int i = 0; i < rowCount; i++)
                    {
                        int ID = (int)this.DataGridView.SelectedRows[i].Cells["編號"].Value;
                        var q = from u in db.message_board
                                     where u.message_id == ID
                                     select u;

                        foreach (var hide in q)
                        {
                            if (hide.message_visible == true)
                            {
                                hide.message_visible = false;
                            }
                            else
                            {
                                hide.message_visible = true;
                            }
                        }
                    }
                    db.SubmitChanges();
                    DataGridViewFill();
                }

            }
            else
            {
                //...
            }
            
            

        }



        #region 按鈕樣式
        private void btnhide_MouseEnter(object sender, EventArgs e)
        {
            this.btnhide.FlatAppearance.BorderSize = 0;
            this.btnhide.ForeColor = Color.White;
        }

        private void btnhide_MouseLeave(object sender, EventArgs e)
        {
            this.btnhide.FlatAppearance.BorderSize = 1;
            this.btnhide.ForeColor = Color.Black;
        }

        private void btnSelect_MouseEnter(object sender, EventArgs e)
        {
            this.btnSelect.FlatAppearance.BorderSize = 0;
            this.btnSelect.ForeColor = Color.White;
        }

        private void btnSelect_MouseLeave(object sender, EventArgs e)
        {
            this.btnSelect.FlatAppearance.BorderSize = 1;
            this.btnSelect.ForeColor = Color.Black;
        }

        private void btnReport_MouseEnter(object sender, EventArgs e)
        {
            this.btnReport.FlatAppearance.BorderSize = 0;
            this.btnReport.ForeColor = Color.White;
        }

        private void btnReport_MouseLeave(object sender, EventArgs e)
        {
            this.btnReport.FlatAppearance.BorderSize = 1;
            this.btnReport.ForeColor = Color.Black;
        }

        #endregion
    }
}
